unit uMainFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,

  GlScene, GlObjects, GlCadencer, GLWin32Viewer, GLRenderContextInfo, GlWindowsFont, GlHudObjects,
  GlMaterial,
  VectorGeometry,
  uTweener,

  uBasePage,
  uGLBasePage,
  uPageManager,
  uOptionsPage,
  uCreditsPage,
  uHelpPage,
  uProfilePage,
  uBaseMainMenu;

type
  TfrmMain = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    {Main Objects}
    fGlScene: TGlScene;
    fCadencer: TGLCadencer;
    fViewer: TGLSceneViewer;
    fCamera: TGlCamera;
    fMainDummy: TGlDummyCube;
    fFpsTimer: Single;
    fMatLib: TGlMaterialLibrary;

    {Text Objects}
    fWinFont: TGLWindowsBitmapFont;
    fHudText: TGLHUDText;
    fBack: TGlHudSprite;

    fPageManager: TPageManager;
    fPages: Array[0..4] of TGLBasePageClass;
    fPageIndex: integer;
    fPageReverse: Boolean;
    fLastPop: TBasePage;

    MousePressed: Boolean;
    SpacePressed: Boolean;
  protected
  public
    Procedure InitFontAndSprites;
    Procedure InitMaterials;
    Procedure InitPageManager;
    Procedure OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
  end;

var
  frmMain: TfrmMain;

implementation
uses
  glKeyboard,
  GLCrossPlatform,
  gltexture,
  uDemoInit;
{$R *.dfm}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  fFpsTimer := 0;
  CreateDemo(self, fGlScene, fCadencer, fViewer);
  
  fCamera := TGlCamera.CreateAsChild(fGlScene.Objects);
  with fCamera do
  begin
    Position.SetPoint(0, 0, 5);
    Direction.SetVector(0, 0, -1);
    Up.SetVector(0, 1, 0);
    CameraStyle := csOrthogonal;
    SceneScale := 0.1;
  end;

  fMainDummy := TGLDummyCube.CreateAsChild(fGlScene.Objects);
  fMainDummy.ObjectsSorting := osNone;

  fViewer.Camera := fCamera;
  fCadencer.OnProgress := OnCadencerProgress;

  fMatLib := TGlMaterialLibrary.Create(self);

  InitMaterials;
  InitFontAndSprites;
  InitPageManager;
end;

procedure TfrmMain.FormActivate(Sender: TObject);
begin
  fCadencer.Enabled := true;
end;

Procedure TfrmMain.InitFontAndSprites;
begin
  fWinFont := TGLWindowsBitmapFont.Create(self);
  fWinFont.Font.Name := 'Arial';

  fBack := TGlHudSprite.CreateAsChild(fMainDummy);
  with fBack do
  begin
    Material.LibMaterialName := 'back_1';
    Material.MaterialLibrary := fMatLib;
    Width := Material.GetActualPrimaryTexture.Image.Width;
    Height := Material.GetActualPrimaryTexture.Image.Height;
    Position.SetPoint(Width / 2, Height / 2, 0);
  end;

  fHudText := TGLHUDText.CreateAsChild(fMainDummy);
  with fHudText do
  begin
    BitmapFont := fWinFont;
    Text := 'Lampogolovii presents '+ #13#10 + '''MenuPages Demo''';
    ModulateColor.SetColor(1, 1, 1, 0.7);
    Position.SetPoint(5, 365, 0);
    Alignment := taLeftJustify;
  end;

end;

Procedure TfrmMain.InitMaterials;
begin
  AddMaterials(fMatLib, str_ImagesFolder + '\MenuPages\', ['back_1'], ['back_1']);
  AddMaterials(fMatLib, str_ImagesFolder + '\MenuPages\', ['cup', 'medkit', 'pillow', 'mittens', 'sneakers'], ['cup', 'medkit', 'pillow', 'mittens', 'sneakers']);
end;

Procedure TfrmMain.InitPageManager;
var
  i: integer;
begin
  fPageIndex := 0;
  fPageReverse := false;
  fPages[0] := TBaseMainMenu;
  fPages[1] := TOptionsPage;
  fPages[2] := TCreditsPage;
  fPages[3] := THelpPage;
  fPages[4] := TProfilePage;

  fPageManager := TPageManager.Create(fMainDummy, fMatLib);
  for i := low(fPages) to high(fPages) do
    fPageManager.RegisterPage(fPages[i]);
end;

Procedure TfrmMain.OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
var
  MPos: TPoint;
  fCurPop: TBasePage;
begin
  GetCursorPos(Mpos);
  MPos := fViewer.ScreenToClient(MPos);

  if not SpacePressed and IsKeyDown(vk_Space) then
  begin
    if fPageReverse then
    begin
      fCurPop := fPageManager.Pop();
      if(fLastPop <> fCurPop) then
      begin
        fLastPop := fCurPop;
        fPageIndex := fPageIndex - 1;
        if fPageIndex = low(fPages) then
          fPageReverse := false;
      end;
    end
    else
    begin
      fPageManager.Push(fPages[fPageIndex]);
      fPageIndex := fPageIndex + 1;

      if fPageIndex > high(fPages) then
        fPageReverse := true;
    end;
  end;

  MousePressed := IsKeyDown(vk_LButton);
  SpacePressed := IsKeyDown(vk_Space);
  fPageManager.Update(deltaTime);
end;

end.

